﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UCNKompetenceBors.Database;
using UCNKompetenceBors.Model;

namespace UCNKompetenceBors
{
    public partial class newsDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData(Convert.ToInt32(Request.QueryString["nid"]));
            }
        }

        /// <summary>
        /// Binds the controls with data from the database, based on the newsId parameter.
        /// </summary>
        /// <param name="newsId">Specifies the ID of the news article to be bound.</param>
        private void BindData(int newsId)
        {

            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            News news = dbNews.GetNewsById(newsId);

            if (culture.Equals("en-GB"))
            {
                lblNewsText.Text = news.LocalizedText.Replace("<br />", Environment.NewLine);
                lblNewsTitle.Text = news.LocalizedTitle;
            }                
            else
            {
                lblNewsText.Text = news.Text;//.Replace("<br />", Environment.NewLine);
                lblNewsTitle.Text = news.Title;
            }

            lblNewsDetailsDateAndUser.Text = news.CreatedDate.ToString("dd-MM-yyyy") + ", " + news.User.UserName;
            imgNewsImage.ImageUrl = news.ImageURL;
        }
    }
}